using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Tetris
{
    class Board
    {
        public bool [,]tiles;
        Size size;
        
        public Size Size
        {
            get { return size; }
        }
	
        public Board(Size s)
        {
            size = s;
            size.Width += (Engine.Margin << 1);
            size.Height += Engine.Margin;
            tiles = new bool[size.Width, size.Height];
            for (int x = 0; x < size.Width; x++)
                for (int y = 0; y < size.Height; y++)
                    if ((x >= Engine.Margin) && (x < size.Width - Engine.Margin) && (y >= Engine.Margin))
                        tiles[x, y] = false;
                    else
                        tiles[x, y] = true;
        }

        public Board(Board b)
        {
            size = b.size;
            tiles = (bool[,]) b.tiles.Clone();
        }

        public Board AddBlock(Block block, Point blockLocation)
        {           
            for (int x = 0; x < 4; x++)
                for (int y = 0; y < 4; y++)
                    if ((x + blockLocation.X >= Engine.Margin) && (x + blockLocation.X < size.Width - Engine.Margin) &&
                        (y + blockLocation.Y >= Engine.Margin) && (y + blockLocation.Y < size.Height))
                        tiles[x + blockLocation.X, y + blockLocation.Y] |= block.tiles[x, y];
            return this;
        }

        public int RemoveLines()
        {
            int linesDisintegrated = 0;
            int max = size.Height;
            bool isLine;
            for (int y = Engine.Margin; y < max; y++)
            {
                isLine = true;
                for (int x = Engine.Margin; x < size.Width - Engine.Margin; x++)
                {
                    if (!tiles[x, y])
                    {
                        isLine = false;
                        break;
                    }
                }
                if(isLine)
                {
                    int z;
                    for (z = y; z < max - 1; z++)
                    {
                        for (int x = Engine.Margin; x < size.Width - Engine.Margin; x++)
                            tiles[x, z] = tiles[x, z + 1];
                    }
                    for (int x = Engine.Margin; x < size.Width - Engine.Margin; x++)
                        tiles[x, z] = false;
                    y--;
                    max--;
                    linesDisintegrated++;
                }
            }
            return linesDisintegrated;
        }

        public int GetMaxHeight()
        {
            int res = -1;

            for (int x = Engine.Margin; x < size.Width - Engine.Margin; x++)
                for (int y = size.Height - 1; (y > res) && (y >= Engine.Margin); y--)
                    if (tiles[x, y] == true)
                        res = y;   

            return res - Engine.Margin;
        }
    }
}
